package com.cosmoplat.xikai.business.domain;

import com.baomidou.mybatisplus.annotation.TableField;
import com.cosmoplat.xikai.common.core.domain.BaseEntity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * 派工任务表 (dispatch_task)
 *
 * @TableName base_dispatch_task
 */
@Slf4j
@Data
@ApiModel(value = "派工任务信息", description = "用于表示系统中的派工任务信息")
public class BaseDispatchTask extends BaseEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    @ApiModelProperty(value = "主键", example = "1")
    private Long id;

    /**
     * 任务ID
     */
    @ApiModelProperty(value = "任务ID", example = "TASK001")
    @NotBlank(message = "任务ID不能为空")
    private String taskId;
//mes
    /**
     * 任务号
     */
    @ApiModelProperty(value = "任务号", example = "TASKNO001")
    @NotBlank(message = "任务号不能为空")
    private String taskNo;
//mes
    /**
     * 工单工序ID
     */
    @ApiModelProperty(value = "工单工序ID", example = "WOPROCESS001")
    private String workOrderProcessId;

    /**
     * 工单号
     */
    @ApiModelProperty(value = "工单号", example = "WORKORDER001")
    //@NotBlank(message = "工单号不能为空")
    private String workOrderNo;

    /**
     * 工作中心ID
     */
    @ApiModelProperty(value = "工作中心ID", example = "WORKCENTER001")
    private String workCenterId;

    /**
     * 工作中心编号
     */
    @ApiModelProperty(value = "工作中心编号", example = "WC001")
    private String workCenterCode;

    /**
     * 工作中心名称
     */
    @ApiModelProperty(value = "工作中心名称", example = "加工中心A")
    private String workCenterName;

    /**
     * 工序ID
     */
    @ApiModelProperty(value = "工序ID", example = "PROCESS001")
    private String processId;

    /**
     * 工序编号
     */
    @ApiModelProperty(value = "工序编号", example = "PROC001")
    private String processCode;

    /**
     * 工序名称
     */
    @ApiModelProperty(value = "工序名称", example = "装配工序")
    private String processName;

    /**
     * 物料ID
     */
    @ApiModelProperty(value = "物料ID", example = "MAT001")
    private String materialId;

    /**
     * 物料编号
     */
    @ApiModelProperty(value = "物料编号", example = "CODE001")
    private String materialCode;

    /**
     * 物料名称
     */
    @ApiModelProperty(value = "物料名称", example = "示例物料")
    private String materialName;

    /**
     * 物料型号
     */
    @ApiModelProperty(value = "物料型号", example = "MODEL-A")
    private String materialModel;

    /**
     * 计划投产时间
     */
    @ApiModelProperty(value = "计划投产时间", example = "2025-01-01 09:00:00")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @NotNull(message = "计划投产时间不能为空")
    private Date planStartTime;

    /**
     * 计划产出时间
     */
    @ApiModelProperty(value = "计划产出时间", example = "2025-01-01 09:00:00")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @NotNull(message = "计划产出时间不能为空")
    private Date planEndTime;

    /**
     * 派工数量
     */
    @ApiModelProperty(value = "派工数量", example = "100.00")
    @NotNull(message = "派工数量不能为空")
    private BigDecimal dispatchQty;

    /**
     * 任务状态，默认值为0
     * NoStarted,未开始;Executing,执行中;Finished,已完成
     */
    @ApiModelProperty(value = "任务状态: NoStarted,未开始;Executing,执行中;Finished,已完成", example = "0")
    private String taskStatus;

    /**
     * 创建时间
     */
    @ApiModelProperty(value = "创建时间", hidden = false)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createdAt;

    /**
     * 修改时间
     */
    @ApiModelProperty(value = "修改时间", hidden = false)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date modifiedAt;

    /**
     * 派工单ID
     */
    @ApiModelProperty(value = "派工单ID", example = "ORDER001")
    private String orderId;

    /**
     * 任务生产状态：0:未开始 1:进行中 2:已完成  3:暂停
     **/
    @ApiModelProperty(value = "生产状态，默认值为:0:未开始 1:进行中 2:已完成  3:暂停", example = "0")
    private Integer taskPlanStatus;

    @ApiModelProperty(value = "暂存上次任务计划状态，默认值为:0:未开始 1:进行中 2:已完成  3:暂停", example = "0")
    private Integer lastTaskPlanStatus;


    /**
     * 实际开始时间
     */
    @ApiModelProperty(value = "任务实际开始时间", example = "2025-01-02 09:00:00")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date taskStartTime;

    /**
     * 实际结束时间
     */
    @ApiModelProperty(value = "任务实际结束时间", example = "2025-01-06 17:00:00")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date taskEndTime;

    /**
     * 已完成数量
     */
    @ApiModelProperty(value = "已完成数量", example = "300.00")
    private BigDecimal completedQty;


    /**
     * 操作人员
     */
    @ApiModelProperty(value = "操作人员工号", required = false, example = "123456789")
    private String operator;

    /**
     * 下发人员
     */
    @ApiModelProperty(value = "下发人员工号", required = false, example = "987654321")
    private String issuer;


    /**
     * 逻辑删除标志，默认未删除
     */
    @ApiModelProperty(value = "逻辑删除标志，默认未删除", example = "false")
    private Boolean isDelete;

    /**
     * 配送信息
     **/
    @ApiModelProperty(value = "配送信息")
    @TableField(exist = false)
    @NotEmpty(message = "派工单配送信息列表不能为空")
    private List<BaseDeliveryInfo> deliveryInfo;


    /**
     * 获取工时，单位小时
     *
     * @return java.math.BigDecimal
     * @date 10:31 2025/1/15
     **/
    public BigDecimal getWorkingHours() {
        if (this.taskStartTime != null && this.taskEndTime != null) {
            BigDecimal value = BigDecimal.valueOf(Math.abs(this.taskStartTime.getTime() - this.taskEndTime.getTime()) / 1000 / 60 / 60);
            log.info("计算工时结果：{}", value);
            return value;
        }
        return null;
    }
}
